using System.Collections;

namespace Game.Core
{
	public class EventObject : IEventObject, IObject
	{
		private Hashtable events;

		private string name;

		public virtual string Name
		{
			get
			{
				return name;
			}
		}

		public EventObject()
			: this("EventObject")
		{
		}

		public EventObject(string name)
		{
			this.name = name;
			events = new Hashtable();
		}

		public virtual bool Bind(string eventName, EventObjectHandler handler)
		{
			if (!events.ContainsKey(eventName))
			{
				events.Add(eventName, ArrayList.Synchronized(new ArrayList()));
			}
			ArrayList arrayList = events[eventName] as ArrayList;
			if (!arrayList.Contains(handler))
			{
				arrayList.Add(handler);
				return true;
			}
			return false;
		}

		public virtual bool UnBind(string eventName, EventObjectHandler handler)
		{
			if (events.ContainsKey(eventName))
			{
				ArrayList arrayList = events[eventName] as ArrayList;
				int num = arrayList.IndexOf(handler);
				if (num > -1)
				{
					arrayList.RemoveAt(num);
					return true;
				}
			}
			return false;
		}

		public virtual bool Fire(BaseEvent evt)
		{
			string eventName = evt.EventName;
			if (!events.ContainsKey(eventName))
			{
				return false;
			}
			ArrayList arrayList = (events[eventName] as ArrayList).Clone() as ArrayList;
			int count = arrayList.Count;
			evt.sender = this;
			EventObjectHandler eventObjectHandler;
			for (int i = 0; i < count; i++)
			{
				eventObjectHandler = arrayList[i] as EventObjectHandler;
				eventObjectHandler(evt);
			}
			eventObjectHandler = null;
			return true;
		}

		public virtual bool HasEvent(string eventName)
		{
			return events.ContainsKey(eventName);
		}

		public virtual void Init()
		{
		}

		public virtual void Destroy()
		{
			events.Clear();
			events = null;
			name = null;
		}
	}
}
